package com.hxalex.simulationhandle.accessbilityService;

import android.accessibilityservice.AccessibilityService;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.accessbilityService.command.base.BaseCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.CheckBrowserIsMainCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.CheckMatchWebSiteIsLoadCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.ClearAllPageCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.EditSearchContentCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.SearchBaiduInfoCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.SearchWebContentCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.SearchWebContentCountCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.UpdateWebSiteCommand;
import com.hxalex.simulationhandle.accessbilityService.command.wifi.BackWifiInMainCommand;
import com.hxalex.simulationhandle.accessbilityService.command.wifi.CheckWifiInMainCommand;
import com.hxalex.simulationhandle.accessbilityService.command.wifi.CheckWifiIsConnectCommand;
import com.hxalex.simulationhandle.accessbilityService.command.wifi.ConnectWifiCommand;
import com.hxalex.simulationhandle.accessbilityService.command.wifi.SearchWifiCommand;
import com.hxalex.simulationhandle.accessbilityService.manger.CommandManger;
import com.hxalex.simulationhandle.accessbilityService.manger.WifiManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.search.SearchWebContentReceiver;
import com.hxalex.simulationhandle.accessbilityService.receiver.search.serivce.ClickWebContentService;
import com.hxalex.simulationhandle.accessbilityService.receiver.wifi.BackWifiInMainReceiver;
import com.hxalex.simulationhandle.accessbilityService.receiver.wifi.ConnectWifiReceiver;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by hx_Alex on 2018/1/24.
 */

public class HandleService extends AccessibilityService
        implements ConnectWifiReceiver.onClickItemListener, BackWifiInMainReceiver.onBackToAppMainListener,
        ClickWebContentService.toBackPreWebSiteListener, SearchWebContentReceiver.toBackPreWebSiteListener {
    public static boolean isStartService = false;
    private static final String TAG = "HandleService";
    private WifiManger wifiManger;
    private CommandManger commandManger = CommandManger.getManger();
    private ArrayBlockingQueue<BaseCommand> commandQueue = commandManger.getCommandQueue();
    private boolean isLoadCommand = false;
    private boolean isCommandOver = false;

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        String packageName = accessibilityEvent.getPackageName().toString();
        if (!isStartService) return;
        //wifi助手逻辑处理
        if (packageName.equals(getResources().getString(R.string.wifi_app_package_name))) {

            AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
            if (rootInActiveWindow != null) {
                recycleWifiApp(rootInActiveWindow, packageName);
            }
            if (!commandQueue.isEmpty()) {
                BaseCommand command = commandQueue.peek();
                if (command instanceof BackWifiInMainCommand) {
                    ((BackWifiInMainCommand) command).setOnBackToAppMainListener(this);
                    command.execute();
                    if (command.isComplete) commandQueue.poll();
                }
            }
        }
         //浏览器逻辑处理
        if (packageName.equals(getResources().getString(R.string.browser_app_package_name))) {
            AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
            if (rootInActiveWindow != null) {
                recycleUcMobile(rootInActiveWindow, packageName);
            }
        }

    }


    public void recycleWifiApp(AccessibilityNodeInfo info, String packageName) {

        if (info.getChildCount() == 0) {
            if (packageName.equals(getResources().getString(R.string.wifi_app_package_name))) {
                if (info.getText() != null) {
                    String text = info.getText() != null ? info.getText().toString() : "";
                    if (!commandQueue.isEmpty()) {
                        isCommandOver = true;
                        BaseCommand command = commandQueue.peek();
                        if (command instanceof CheckWifiInMainCommand) {
                            ((CheckWifiInMainCommand) command).setText(text);
                        } else if (command instanceof ConnectWifiCommand) {
                            ((ConnectWifiCommand) command).setOnClickItemListener(this);
                        }
                        command.execute();
                        if (command.isComplete) commandQueue.poll();
                    } else {
//                        if(isCommandOver)
//                        Log.i(TAG, "任务执行完毕");
                        isCommandOver = false;
                    }
                }
            }
        } else {
//            Log.e(TAG,info.toString());
            searchWifiTab(info);
            if (!commandQueue.isEmpty()) {
                BaseCommand command = commandQueue.peek();
                if (command instanceof SearchWifiCommand) {
                    ((SearchWifiCommand) command).setNowInfo(info);
                    command.execute();
                    if (command.isComplete) commandQueue.poll();
                } else if (command instanceof CheckWifiIsConnectCommand) {

                    ((CheckWifiIsConnectCommand) command).setNodeInfo(info);
                    command.execute();
                    if (command.isComplete) commandQueue.poll();
                }
            }

            for (int i = 0; i < info.getChildCount(); i++) {
                if (info.getChild(i) != null) {
                    recycleWifiApp(info.getChild(i), packageName);
                }
            }
        }
    }


    public void recycleUcMobile(AccessibilityNodeInfo info, String packageName) {

        if (info.getChildCount() == 0) {
            
            if (!commandQueue.isEmpty()) {
                BaseCommand command = commandQueue.peek();
                if (command instanceof UpdateWebSiteCommand) {
                    ((UpdateWebSiteCommand) command).setNodeInfo(info);

                } else if (command instanceof CheckBrowserIsMainCommand) {
                    ((CheckBrowserIsMainCommand) command).setNodeInfo(info);

                } else if (command instanceof EditSearchContentCommand) {
                    ((EditSearchContentCommand) command).setNodeInfo(info);
                } else if (command instanceof SearchBaiduInfoCommand) {
                    ((SearchBaiduInfoCommand) command).setNodeInfo(info);
                } else if (command instanceof SearchWebContentCommand) {
                    ((SearchWebContentCommand) command).setNodeInfo(info);
                    ((SearchWebContentCommand) command).setToBackPreWebSiteListener(this);
                } else if (command instanceof SearchWebContentCountCommand) {
                    ((SearchWebContentCountCommand) command).setNodeInfo(info);
                } else if (command instanceof ClearAllPageCommand) {
                    ((ClearAllPageCommand) command).setNodeInfo(info);
                }
                command.execute();
                if (command.isComplete) commandQueue.poll();
            } else {
//            Log.i(TAG, "任务执行完毕");
            }
        } else {

            if (!commandQueue.isEmpty()) 
            {
                BaseCommand command = commandQueue.peek();
                if (command instanceof CheckMatchWebSiteIsLoadCommand) {
                    ((CheckMatchWebSiteIsLoadCommand) command).setNodeInfo(info);
                    command.execute();
                    if (command.isComplete) commandQueue.poll();
                }
            }
            for (int i = 0; i < info.getChildCount(); i++) {
                if (info.getChild(i) != null) {
                    recycleUcMobile(info.getChild(i), packageName);
                }
            }
        }


    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();

    }
    
    private void searchWifiTab(AccessibilityNodeInfo nodeInfo) {

        if (wifiManger == null) wifiManger = WifiManger.getWifiManger(this);
        wifiManger.searchWifiTab(nodeInfo);
    }


    @Override
    public void onInterrupt() {
        Log.i(TAG, "onInterrupt");
    }


    @Override
    public void onclickItem(int EventType) {
        if (wifiManger == null) wifiManger = WifiManger.getWifiManger(this);
        if (EventType == ConnectWifiReceiver.TYPE_CLICK) {
            wifiManger.setWifiAppRootInfo(getRootInActiveWindow());
        } else if (EventType == ConnectWifiReceiver.TYPE_BACK) {
            Log.e(TAG, "回退弹窗......");
            performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
        }

    }


    @Override
    public void onBackToApp() {
        Log.e(TAG, "回退至主页面......");
        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
    }

    private boolean isHaveConnectWindow(AccessibilityNodeInfo info) {
        boolean reslut = false;
        if (info == null || info.getClassName() == null || info.getViewIdResourceName() == null)
            return reslut;
        else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if (info.getChild(i) != null) {
                    isHaveConnectWindow(info.getChild(i));
                }
            }
        }
        Log.i(TAG, "reslut:" + reslut);
        return reslut;
    }


    @Override
    public void toBackPreWebSite() {
        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
        Log.e(TAG, "回退....");
    }

    @Override
    public void toBackPreWebSiteToSearchWebContent() {
        Log.e(TAG, "回退....");
        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
    }
    
}
